/*
 * Created by JFormDesigner on Mon Jul 01 20:01:21 CST 2024
 */

package scct.ZhuZhouXing.ui;

import jdk.nashorn.internal.scripts.JO;
import scct.ZhuZhouXing.dao.ProductDao;
import scct.ZhuZhouXing.dao.UserDao;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author xzy
 */
public class DlgProductAdd extends JDialog {
    public DlgProductAdd(Window owner) {
        super(owner);
        initComponents();
    }


    private void btnAdd(ActionEvent e) {
        String name = txtname.getText();
        int price = Integer.parseInt(txtprice.getModel().getValue().toString());
        int inventory = Integer.parseInt(txtinventory.getModel().getValue().toString());
        String leibie = (String) comleibie.getSelectedItem();
        String brisf = txtbrief.getText();
        if (name.isEmpty() || !name.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的商品名称");
            return;
        }
        if (price < 1 || price > 30000) {
            JOptionPane.showMessageDialog(this, "请输入合法的价格1~30000！");
            return;
        }
        if (inventory < 1 || inventory >= 20) {
            JOptionPane.showMessageDialog(this, "数量必须小于20！");
            return;
        }
        if (leibie.isEmpty() || !leibie.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法种类");
            return;
        }
        if (brisf.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入简介");
            return;
        }
        ProductDao productDao = new ProductDao();
//        判断是否重复
        if (productDao.isDupname(name)) {
            JOptionPane.showMessageDialog(this, "您输入的商品名称已存在！");
            return;
        }
        if (productDao.addProduct(name, price, inventory, leibie, brisf)) {
            JOptionPane.showMessageDialog(this, "添加成功！");
            PaneProductManager.getTable();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "添加失败！");
        }
    }

    private void btncnel(ActionEvent e) {
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 老登
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        txtname = new JTextField();
        scrollPane1 = new JScrollPane();
        txtbrief = new JTextArea();
        label6 = new JLabel();
        btnAdd = new JButton();
        btncnel = new JButton();
        txtinventory = new JSpinner();
        comleibie = new JComboBox<>();
        txtprice = new JSpinner();

        //======== this ========
        setTitle("\u5546\u54c1\u6dfb\u52a0");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u5546\u54c1\u540d\u79f0");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 3f));
        contentPane.add(label1);
        label1.setBounds(100, 70, 71, 30);

        //---- label2 ----
        label2.setText("\u5546\u54c1\u4ef7\u683c");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 3f));
        contentPane.add(label2);
        label2.setBounds(100, 120, 71, 29);

        //---- label3 ----
        label3.setText("\u5546\u54c1\u6570\u91cf");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 3f));
        contentPane.add(label3);
        label3.setBounds(100, 170, 71, 28);

        //---- label4 ----
        label4.setText("\u5546\u54c1\u79cd\u7c7b");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 3f));
        contentPane.add(label4);
        label4.setBounds(100, 225, 71, 27);

        //---- label5 ----
        label5.setText("\u5546\u54c1\u7b80\u4ecb");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 3f));
        contentPane.add(label5);
        label5.setBounds(100, 275, 71, 31);
        contentPane.add(txtname);
        txtname.setBounds(185, 70, 220, 30);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(txtbrief);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(185, 280, 220, 120);

        //---- label6 ----
        label6.setText("\u5546\u54c1\u6dfb\u52a0");
        label6.setFont(label6.getFont().deriveFont(label6.getFont().getSize() + 14f));
        contentPane.add(label6);
        label6.setBounds(230, -5, 145, 65);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(160, 430, 85, 40);

        //---- btncnel ----
        btncnel.setText("\u53d6\u6d88");
        btncnel.addActionListener(e -> btncnel(e));
        contentPane.add(btncnel);
        btncnel.setBounds(315, 430, 85, 40);

        //---- txtinventory ----
        txtinventory.setFont(txtinventory.getFont().deriveFont(txtinventory.getFont().getSize() + 2f));
        contentPane.add(txtinventory);
        txtinventory.setBounds(185, 175, 140, 35);

        //---- comleibie ----
        comleibie.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u98df\u54c1\u96f6\u98df",
            "\u996e\u7528\u77ff\u6cc9\u6c34",
            "\u5bb6\u7528\u7535\u5668",
            "\u6d17\u6f31\u7528\u54c1",
            "\u852c\u83dc\u679c\u852c",
            "\u6d77\u9c9c\u4ea7\u54c1",
            "\u8c03\u5473\u54c1",
            "\u7535\u5b50\u4ea7\u54c1"
        }));
        contentPane.add(comleibie);
        comleibie.setBounds(190, 225, 125, 40);

        //---- txtprice ----
        txtprice.setFont(txtprice.getFont().deriveFont(txtprice.getFont().getSize() + 2f));
        contentPane.add(txtprice);
        txtprice.setBounds(185, 120, 140, 35);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(595, 575);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 老登
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField txtname;
    private JScrollPane scrollPane1;
    private JTextArea txtbrief;
    private JLabel label6;
    private JButton btnAdd;
    private JButton btncnel;
    private JSpinner txtinventory;
    private JComboBox<String> comleibie;
    private JSpinner txtprice;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
